package cn.edu.cqupt.cyExam.controller;



import cn.edu.cqupt.cyExam.myBatis.bean.Page;
import cn.edu.cqupt.cyExam.myBatis.bean.User;
import cn.edu.cqupt.cyExam.myBatis.dao.UserMapper;

import cn.edu.cqupt.cyExam.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class JDBCController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/queryUserList")
    public JSONResult queryUserList(@RequestParam(value="pagenum",required=false) String pagenum,
                                    @RequestParam(value="pagesize",required=false) String pagesize,
                                    HttpSession session,
                                    HttpServletRequest request){
        System.out.print(pagenum+"num----");
        System.out.print(pagesize+"size___-");
        Integer Pagenum = 0;
        Integer Pagecount = 5;
        if(pagenum != null){
            Pagenum = Integer.parseInt(pagenum) - 1;
        }
        if(pagesize != null){
            Pagecount = Integer.parseInt(pagesize);
        }
        Pagenum = Pagenum * Pagecount;
        Integer usercount = userMapper.countUser();
        Page page = new Page();
        page.setTotalRecord(usercount);
        int totalpage = (usercount/Pagecount) + 1;
        page.setTotalPage(totalpage);

        List<User> users = userMapper.queryUserListByPage(Pagenum,Pagecount);
        page.setData(users);
        page.setPageNumber(Integer.parseInt(pagenum));


        return new JSONResult(0, "用户总数："+usercount+"----总页数："+totalpage, 1, page);

    }
}
